var WxParse = require('../../wxParse/wxParse.js');
var area_picker = require('./../../components/area-picker/area-picker.js');
Page({
    data: {
        name: "",
        detail: "",
        date: "",
        district: null,
        forms_des: "",
        des_info: {
            full: false,
            height: "100rpx",
            text: "展开"
        },
        items: [
            {value: '提供设计', name: '提供设计'},
            {value: '策划方案', name: '策划方案'},
            {value: '公司人员到场', name: '公司人员到场'}
          ],
        items2: [
            {value: '赠品', name: '赠品'},
            {value: '品牌/产品宣传册', name: '品牌/产品宣传册'},
            {value: '宣传物料', name: '宣传物料'}
        ]
    },
    onLoad: function (options) {
        getApp().page.onLoad(this, options);

        var self = this;
        self.getDistrictData(function (data) {
            area_picker.init({
                page: self,
                data: data,
            });
        });
        self.getStore();
    },

    showDes: function(){
        if(this.data.des_info.full){
            this.setData({
                des_info: {
                    full: false,
                    height: "100rpx",
                    text: "展开"
                }
            })
        }else{
            this.setData({
                des_info: {
                    full: true,
                    height: "auto",
                    text: "收起"
                }
            })
        }
    },

    bindDateChange: function(e) {
        console.log('picker发送选择改变，携带值为', e.detail.value)
        this.setData({
          date: e.detail.value
        })
    },

    getDistrictData: function (cb) {
        var district = getApp().core.getStorageSync(getApp().const.DISTRICT);
        if (!district) {
            getApp().core.showLoading({
                title: "正在加载",
                mask: true,
            });
            getApp().request({
                url: getApp().api.default.district,
                success: function (res) {
                    getApp().core.hideLoading();
                    if (res.code == 0) {
                        district = res.data;
                        getApp().core.setStorageSync(getApp().const.DISTRICT,district);
                        cb(district);
                    }
                }
            });
            return;
        }
        cb(district);
    },

    onAreaPickerConfirm: function (e) {
        var self = this;
        self.setData({
            district: {
                province: {
                    id: e[0].id,
                    name: e[0].name,
                },
                city: {
                    id: e[1].id,
                    name: e[1].name,
                },
                district: {
                    id: e[2].id,
                    name: e[2].name,
                },
            }
        });
    },

    saveAddress: function () {
        var self = this;
        // var myreg = /^([0-9]{6,12})$/;
        // var myreg2 = /^(\d{3,4}-\d{6,9})$/;
        // var myreg3 = /^\+?\d[\d -]{8,12}\d/;
        // if (!myreg.test(self.data.mobile) && !myreg2.test(self.data.mobile) && !myreg3.test(self.data.mobile)) {
        //     self.showToast({
        //         title: "联系电话格式不正确",
        //     });
        //     return false;
        // }
        getApp().core.showLoading({
            title: "正在保存",
            mask: true,
        });
        var district = self.data.district;
        if (!district) {
            district = {
                province: {
                    name: ""
                },
                city: {
                    name: ""
                },
                district: {
                    name: ""
                }
            };
        }

        var items_arr = self.data.items;
        var items = "";
        for (let j = 0, lenJ = items_arr.length; j < lenJ; ++j) {
            if (items_arr[j].checked === true) {
                items += items_arr[j].value + " ";
            }
          }

        var items_arr2 = self.data.items2;
        var items2 = "";
        for (let j = 0, lenJ = items_arr2.length; j < lenJ; ++j) {
            if (items_arr2[j].checked === true) {
                items2 += items_arr2[j].value + " ";
            }
          }

        var  a = {data: {
            name: self.data.name,
            adddate: self.data.date,
            province: district.province.name,
            city: district.city.name,
            district: district.district.name,
            detail: self.data.detail,
            items: items,
            items2: items2,
        }}

        console.log(a);
        var my_access_token = getApp().core.getStorageSync(getApp().const.ACCESS_TOKEN);

        getApp().request({
            url: "https://wxapp.yzhihe.com/wxappweb/applysave.html?my_access_token="+my_access_token,
            method: "post",
            data: {
                name: self.data.name,
                adddate: self.data.date,
                province: district.province.name,
                city: district.city.name,
                district: district.district.name,
                detail: self.data.detail,
                items: items,
                items2: items2,
            },
            success: function (res) {
                getApp().core.hideLoading();
                if (res.errcode == 0) {
                    getApp().core.showModal({
                        title: "提示",
                        content: "申请提交成功",
                        showCancel: false,
                        success: function (res) {
                            if (res.confirm) {
                                getApp().core.navigateBack();
                            }
                        }
                    });
                }
                if (res.code == 1) {
                    self.showToast({
                        title: res.msg,
                    });
                }
            }
        });
    },

    checkboxChange(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    
        const items = this.data.items
        const values = e.detail.value
        for (let i = 0, lenI = items.length; i < lenI; ++i) {
          items[i].checked = false
    
          for (let j = 0, lenJ = values.length; j < lenJ; ++j) {
            if (items[i].value === values[j]) {
              items[i].checked = true
              break
            }
          }
        }
        this.setData({
          items
        })
    },

    checkboxChange2(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    
        const items2 = this.data.items2
        const values2 = e.detail.value
        for (let i = 0, lenI = items2.length; i < lenI; ++i) {
          items2[i].checked = false
    
          for (let j = 0, lenJ = values2.length; j < lenJ; ++j) {
            if (items2[i].value === values2[j]) {
              items2[i].checked = true
              break
            }
          }
        }
        this.setData({
          items2
        })
    },

    inputBlur: function (e) {
        var name = e.currentTarget.dataset.name;
        var value = e.detail.value;
        var data = '{"' + name + '":"' + value + '"}';
        this.setData(JSON.parse(data));
    },

    onShow: function () {
        getApp().page.onShow(this);
    },

    getStore: function() {
        var self = this;
        getApp().request({
            url: getApp().api.default.store,
            success: function(res) {
               console.log(res);
               if(res.code == 0){
                    var detail2 = res.data.store.forms_des;
                    WxParse.wxParse("detail2", "html", detail2, self);            
               }
            }
        });
    }
});